using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Data.SqlClient;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;
using System.IO;
using SiteManager;

// Help File : HealthCare-ReportDocumentation.txt
 
public partial class Admin_reports_Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    { 
        System.String eDt = Request.QueryString["dt"].ToString().Trim();
        try
        {
                                 
            if (eDt.Trim().Length != 0)
            {
                System.String strCriteria = Request.QueryString["crt"].ToString().Trim();  // to split a string into corresponding parameter values for the procedure 
                System.String eFlg = strCriteria.Substring(0, 1);                          // like DAPM string will split down into (D) for doctor, (DAP) Doctor Accessed Profile, & (M) for Monthly
                System.String rType = strCriteria.Substring(0, 3);
                System.String cFlg = strCriteria.Substring(3, 1);
                System.String grdName = BindGrid(eDt, "E", eFlg, rType, cFlg);  // Need to capture the grid to bind it to the excelsheet i.e ProfileGrid(Doctors) or UserGrid(Users)
                ExportGridView(grdName);
            }
            else
            {
                Response.Write("Request Failed : Date=<b><font color=red>" + eDt + "</font></b> <br/>Invalid Date Format");
                ProfileGrid.Visible = false;
            }
            

        }
        catch (Exception)
        {
            Response.Write("Request Failed : Date=<b><font color=red>" + eDt + "</font></b> <br/>Invalid Date Format");
            ProfileGrid.Visible = false;
        }

    }
   
    protected string BindGrid(string eDt, string fbFlg, string eFlg, string rType, string cFlg)
    {
        Manager mgr = new Manager(true,"ConnectionString");
        SqlDataReader drRep;
        SqlParameter[] sp_Array = new SqlParameter[5];
        System.String grdName =string.Empty;
        sp_Array[0] = new SqlParameter("@EntryDt", SqlDbType.Char);
        sp_Array[1] = new SqlParameter("@FBflag", SqlDbType.Char);
        sp_Array[2] = new SqlParameter("@Entity_flag", SqlDbType.Char);
        sp_Array[3] = new SqlParameter("@Rpt_type", SqlDbType.Char);
        sp_Array[4] = new SqlParameter("@Criteria_flag", SqlDbType.Char);

        sp_Array[0].Size = 10;
        sp_Array[1].Size = 1;
        sp_Array[1].Size = 1;
        sp_Array[1].Size = 5;
        sp_Array[1].Size = 1;

        sp_Array[0].Value =eDt;
        sp_Array[1].Value = fbFlg;
        sp_Array[2].Value = eFlg;
        sp_Array[3].Value = rType;
        sp_Array[4].Value = cFlg;

        drRep = mgr.ExecuteQuery("sp_SiteAnalytics", sp_Array);

        if (eFlg == "D")  // Doctors
        {
            ProfileGrid.DataSource = drRep;
            ProfileGrid.DataBind();
            grdName = "ProfileGrid";
        }
        else if (eFlg == "U") // Users
        {
            UserGrid.DataSource = drRep;
            UserGrid.DataBind();
            grdName = "UserGrid";
        }
        
        return grdName;
    }


    // Exporting Data To Excel Starts from here

    
    private void ExportGridView(System.String grdName)
    {
        System.String sheetName=string.Empty;  // Required to change the sheet name based on Criteria (Doctor or User)
        if (grdName == "ProfileGrid")
        {
            sheetName = "DoctorsList.xls";
        }
        else if (grdName == "UserGrid")
        {
            sheetName = "UsersList.xls";
        }
        Response.Clear();
        Response.ClearContent();
        Response.ContentType = "application/vnd.ms-excel";
        Response.AddHeader("Content-Disposition", "attachment;filename=\"" + sheetName + "\"");
        //Response.AddHeader("content-disposition", "attachment;filename="+sheetName);

        // create a string writer
        using (StringWriter stringWrite = new StringWriter())
        {
            using (HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite))
            {
                // instantiate a datagrid
                GridView grdObj = new GridView();
                grdObj = (GridView)Page.FindControl(grdName);
                grdObj.RenderControl(htmlWrite);
                Response.Write(stringWrite.ToString());
                Response.End();
            }
        }
        

        //System.IO.StringWriter stringWrite = new System.IO.StringWriter();

        //System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);

       

        //Response.End();

    }


    public static void ExportDataSetToExcel(DataSet ds, string filename)
    {
        HttpResponse response = HttpContext.Current.Response;

        // first let's clean up the response.object
        response.Clear();
        response.Charset = "";

        // set the response mime type for excel
        response.ContentType = "application/vnd.ms-excel";
        response.AddHeader("Content-Disposition", "attachment;filename=\"" + filename + "\"");

        // create a string writer
        using (StringWriter sw = new StringWriter())
        {
            using (HtmlTextWriter htw = new HtmlTextWriter(sw))
            {
                // instantiate a datagrid
                DataGrid dg = new DataGrid();
                dg.DataSource = ds.Tables[0];
                dg.DataBind();
                dg.RenderControl(htw);
                response.Write(sw.ToString());
                response.End();
            }
        }
    }
 
   
    public override void VerifyRenderingInServerForm(Control control)
    {

    }

  

}
